package com.bot.edu.informationmanage.users.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bot.edu.informationmanage.users.entity.UserInfo;
import com.bot.edu.informationmanage.users.mapper.UserInfoMapper;
import com.bot.edu.informationmanage.users.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpSession;

/**
 * @Description: 用户表
 * @Author: jeecg-boot
 * @Date:   2022-04-02
 * @Version: V1.0
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void sendCode(String tel,String type, HttpSession session) throws Exception {
        QueryWrapper<UserInfo> userInfoQueryWrapperr = new QueryWrapper<>();
        userInfoQueryWrapperr.lambda().eq(UserInfo::getUserPhone,tel);
        UserInfo userInfo = this.getOne(userInfoQueryWrapperr);
        if (!type.equals("0")) {
            if (userInfo==null) {
                throw new Exception("用户不存在");
            }
        }
        Long timeNum = (Long) session.getAttribute("time"+tel);
        if (timeNum!=null){
            if (System.currentTimeMillis()-timeNum<60000){
                throw new Exception("发送过于频繁 请稍后再试");
            }
        }

        Integer code = (int)((Math.random()*9+1)*100000);
        session.setAttribute("code"+tel,code);
        session.setMaxInactiveInterval(120);
        System.out.println(code);
        try {
            /*SmsUtils.sendCode(tel,code);*/
        } catch (Exception e) {
            throw new Exception("发送短信异常"+e.getMessage());
        }

        session.setAttribute("time"+tel,System.currentTimeMillis());
    }
}
